package com.etc.baolema.controller;

import com.etc.baolema.entity.User;
import com.etc.baolema.service.UserService;
import com.etc.baolema.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet({"/user.register","/user.login","/user.checkUser"})
public class UserServlet extends HttpServlet {
    private UserService service= new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String op = uri.substring(uri.indexOf(".")+1);
        Gson gson = new Gson();

        PrintWriter out = response.getWriter();
        if("register".equals(op)){
            String user = request.getParameter("user");
            String upwd = request.getParameter("upwd");
            String phone = request.getParameter("phone");
            User u = new User();
            Map<String, Object> resultMap = new HashMap<>();
            u.setUser(user);
            u.setUpwd(upwd);
            u.setPhone(phone);
            int result = service.addUser(u);
            if(result!=-1){
                resultMap.put("code",200);
                resultMap.put("msg","注册成功");
            }else{
                resultMap.put("code",201);
                resultMap.put("msg","注册失败");
            }
            String json= gson.toJson(resultMap);
            out.print(json);

        }
        if("login".equals(op)){
            String user = request.getParameter("user");
            String upwd = request.getParameter("upwd");
            User u =service.login(user);
            Map<String, Object> resultMap = new HashMap<>();
            if(u==null){
                resultMap.put("code",202);
                resultMap.put("msg","用户名不存在");
            }else if (!upwd.equals(u.getUpwd())){
                resultMap.put("code",201);
                resultMap.put("msg","密码输入错误");
            }else{
                resultMap.put("code",200);
                resultMap.put("msg","登录成功");
                resultMap.put("currentUser",u);
            }
            String json=gson.toJson(resultMap);
            out.print(json);
        }
        if("checkUser".equals(op)){
            String user = request.getParameter("user");
            Map<String, Object> resultMap = new HashMap<>();
            User u = service.login(user);
            if(u==null){
                resultMap.put("code",200);
                resultMap.put("msg","用户名可以使用");
            }else{
                resultMap.put("code",201);
                resultMap.put("msg","用户名已存在");
            }
            String json=gson.toJson(resultMap);
            out.print(json);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
